﻿/// <summary>
/// https://leetcode-cn.com/problems/group-anagrams/
/// </summary>
public class Solution
{
    private string AsTemplateString(string s)
    {
        var cs = s.ToArray();
        Array.Sort(cs);
        return new string(cs.ToArray());
    }

    public IList<IList<string>> GroupAnagrams(string[] strs)
    {
        Dictionary<string, IList<string>> result = new Dictionary<string, IList<string>>();
        foreach (var str in strs)
        {
            var key = this.AsTemplateString(str);
            if (result.ContainsKey(key))
                result[key].Add(str);
            else
                result.Add(key, new List<string>() { str });
        }
        return new List<IList<string>>(result.Values);
    }
}